package com.miracle.common.util;

import android.app.Activity;
import android.os.Environment;
import android.support.v4.app.Fragment;

import com.laojiang.imagepickers.ImagePicker;
import com.laojiang.imagepickers.data.ImagePickType;
import com.laojiang.imagepickers.ui.camera.DiyCameraActivity;
import com.laojiang.imagepickers.utils.GlideImagePickerDisplayer;

import java.io.File;

/**
 * com.miracle.common.util
 * (c)2018 AIR Times Inc. All rights reserved.
 *
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/7/12 14:10
 * @see com.miracle.common.util
 */
public class ImageUtil {
    /**
     * 获取单张图片
     * @param activity
     * @param type 类型
     * @param requestCode
     * @param resultCode
     */
    public static void getSinglePic(Activity activity, ImagePickType type, int requestCode, int
            resultCode) {

        /*
         * 图片/视频选择器
         * pickType：设置选取类型(拍照ONLY_CAMERA、单选SINGLE、多选MUTIL)
         * maxNum：设置最大选择数量(此选项只对多选生效，拍照和单选都是1，修改后也无效)
         * needCamera：是否需要在界面中显示相机入口(类似微信那样)
         * cachePath：自定义缓存路径(拍照和裁剪都需要用到缓存)
         * scanType：查询范围 1.自己app文件夹  2.全部
         * doCrop：裁剪功能需要调用这个方法，多选模式下无效，参数：aspectX,aspectY,outputX,outputY
         * displayer：自定义图片加载器，默认是Glide实现的,可自定义图片加载器
         * needVideo：是否有视频
         */
        ImagePicker build = new ImagePicker.Builder()
                .pickType(type)
                .maxNum(1)
                .needCamera(false)
                .cachePath(Environment.getExternalStorageDirectory().getPath() + File.separator +
                        "MyAppPic")
                .scanType(2)
//                .doCrop(1, 1, 300, 300)
                .displayer(new GlideImagePickerDisplayer())
                .needVideo(false)
                .build();

        if (type == ImagePickType.ONLY_CAMERA) {
            DiyCameraActivity.start(activity, build.getmOptions().getCachePath(), build
                    .getmOptions().isNeedVideo());
        } else {
            build.start(activity, requestCode, resultCode);
        }
    }

    public static void getSinglePic(Fragment fragment, ImagePickType type, int requestCode, int
            resultCode) {
        ImagePicker build = new ImagePicker.Builder()
                .pickType(type)
                .maxNum(1)
                .needCamera(false)
                .cachePath(Environment.getExternalStorageDirectory().getPath() + File.separator +
                        "MyAppPic")
                .scanType(2)
//                .doCrop(1, 1, 300, 300)
                .displayer(new GlideImagePickerDisplayer())
                .needVideo(false)
                .build();
        if (type == ImagePickType.ONLY_CAMERA) {
            DiyCameraActivity.start(fragment, build.getmOptions().getCachePath(),
                    build
                            .getmOptions().isNeedVideo());
        } else {
            build.start(fragment, requestCode, resultCode);
        }
    }
}
